<?php
package("Query");
package("Expression");


class Query_Insert implements Query_Builder {
	/**
	 * @var Expression_Operator_Left
	 */
	private $_insert; // INSERT INTO users (id, name, password) VALUES (null, "john doe", "password")
	
	/**
	 * @var Expression_Operator
	 */
	private $_columns;
	
	/**
	 * @var Expression_Operator
	 */
	private $_values;
	
	public function __construct() {
		$factory = Expression_Operator_Factory::getSingleton();
		$this->_insert = $factory->construct("insert", array());
		
		$this->_columns = $factory->construct("separator", array());
		$this->_values = $factory->construct("separator", array());
	}
	
	public function bindTable($table) {
		$table = new Expression_Operand_Table($table);
		$this->_insert->addOperand($table);
		$factory = Expression_Operator_Factory::getSingleton();
		$column_group = $factory->construct("group", array($this->_columns));
		$this->_insert->addOperand($column_group);
		$value_group = $factory->construct("group", array($this->_values));
		$value = $factory->construct("value", array($value_group));
		$this->_insert->addOperand($value);
	}
	
	public function bindKey(Core_Property $bound) {
		$this->bindProperty($bound);
	}
	
	public function bindProperty(Core_Property $bound) {
		$factory = Expression_Operator_Factory::getSingleton();
		$column= new Expression_Operand($bound->column);
		$this->_columns->addOperand($column);
		
		$value = new Expression_Operand_Variable($bound->value);
		$this->_values->addOperand($value);
	}
	
	public function toSql() {
		return $this->_insert->evaluate();
	}
}
?>